/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import entity.OrdersDetails;
import iface.IManagerDB;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import manager.DisabledOrdersDetailsManager;
import manager.OrdersDetailsManager;

/**
 *
 * @author User
 */
public class OrdersDetailsDB implements IManagerDB<OrdersDetailsManager, DisabledOrdersDetailsManager> {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public OrdersDetailsDB(Connection conn) {
        this.conn = conn;
    }

    @Override
    public void loadData(OrdersDetailsManager t, boolean value) {
        try {
            cstmt = conn.prepareCall("{call sp_GetOrdersDetails(?)}");
            cstmt.setBoolean("Status", value);
            rs = cstmt.executeQuery();

            while (rs.next()) {
                int ordersID = rs.getInt("OrdersID");
                int bookID = rs.getInt("BookID");
                Date returnDate = rs.getDate("ReturnDate");
                String reason = rs.getString("Reason");
                Float money = rs.getFloat("Money");
                boolean status = rs.getBoolean("Status");

                OrdersDetails ordersDetails = new OrdersDetails(ordersID, bookID, returnDate, reason, money, status);
                t.add(ordersDetails);
            }
        } catch (SQLException ex) {
            Logger.getLogger(OrdersDetailsDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void insert(Object object) {
        OrdersDetails ordersDetails = (OrdersDetails) object;
        try {
            cstmt = conn.prepareCall("{call sp_InsertOrdersDetails(?,?,?)}");
            cstmt.setInt(1, ordersDetails.getOrdersID());
            cstmt.setInt(2, ordersDetails.getBookID());
            cstmt.setBoolean(3, ordersDetails.isStatus());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(OrdersDetailsDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Object object) {
        OrdersDetails ordersDetails = (OrdersDetails) object;
        try {
            cstmt = conn.prepareCall("{call sp_UpdateOrdersDetails(?,?,?,?,?,?)}");
            cstmt.setInt(1, ordersDetails.getOrdersID());
            cstmt.setInt(2, ordersDetails.getBookID());
            cstmt.setDate(3, new java.sql.Date(ordersDetails.getReturnDate().getTime()));
            cstmt.setString(4, ordersDetails.getReason());
            cstmt.setFloat(5, ordersDetails.getMoney());
            cstmt.setBoolean(6, ordersDetails.isStatus());

            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(OrdersDetailsDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
